from PyQt5 import QtWidgets,QtGui,QtCore
import sys
from winQQ import Ui_Form
import win32gui
import win32con
import win32clipboard as w
from splinter.browser import Browser
from pypinyin import pinyin,lazy_pinyin
from Pinyin2Hanzi import DefaultDagParams,dag


class mycyjl(QtWidgets.QWidget,Ui_Form):
    def __init__(self):
        super(mycyjl,self).__init__()
        self.setupUi(self)
        self.search_url = "http://www.baidu.com"
        self.driver = Browser(driver_name='chrome',
                         executable_path=r'C:\Users\Administrator\AppData\Local\Programs\Python\Python36\Scripts\chromedriver.exe')

    def sendtoxb(self):
        try:
            # to_who = '木木的 Android手机'
            to_who = '聊聊'
            fullcy = self.send_key.text().strip()
            fullpy = lazy_pinyin(fullcy)
            print('首子的拼音：', fullpy[0])

            item = fullcy[0] + '开头的成语'
            print(item)

            aite='@QQ'
            self.buildaite(to_who,aite)
            # 查询百度的X开头的成语
            msg =self.search_ans(item)
            if msg is None:
                print('默认搜索没搜到 开始查询同音字')
                dagparams=DefaultDagParams()
                resultdc=dag(dag_params=dagparams,pinyin_list=[fullpy[0]],path_num=10,log=True)
                for item in resultdc:
                    print(item.path[0])
                    msg=self.search_ans(item.path[0]+'开头的成语')
                    if msg is not None:
                        break

            self.chengyu.setText(msg)
            self.send_qq(to_who, msg)
        except Exception as e:
            print(e)

    @staticmethod
    def getText():
        """获取剪贴板文本"""
        w.OpenClipboard()
        d = w.GetClipboardData(win32con.CF_UNICODETEXT)
        w.CloseClipboard()
        return d

    @staticmethod
    def setText(aString):
        """设置剪贴板文本"""
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
        w.CloseClipboard()

    @staticmethod
    def send_qq(to_who, msg):
        """设置剪贴板文本"""
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, msg)
        w.CloseClipboard()



        qq = win32gui.FindWindow(None, to_who)
        # win32gui.SendMessage(qq,258,22,2080193)

        # 模拟回车键 选择@QQ小冰
        win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        win32gui.SendMessage(qq, 770, 0, 0)

        win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        #win32gui.SendMessage(qq, win32con.WM_KEYUP, win32con.VK_RETURN, 0)

    def search_ans(self,send_key):
        self.driver.driver.set_window_size(700, 500)
        self.driver.visit(self.search_url)
        # while True:


        # 先取关键字搜索
        self.driver.fill('wd', send_key)
        self.driver.find_by_value('百度一下').click()
        print(self.driver.status_code)
        ele = self.driver.find_by_xpath('//*[@id="1"]/div/div[1]/div[2]/div[1]/div[1]/p/a')
        if len(ele)>0:
            print('获取到元素了')
            print(ele[0].text)
            return ele[0].text
        else:
            print('获取元素失败')
            return None




    """
    模拟@QQ小冰
    """
    @staticmethod
    def buildaite(to_who,astr):
        """设置剪贴板文本 很奇怪把设置剪贴板内容设为单独方法 无法正常执行"""
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, astr)
        w.CloseClipboard()
        qq = win32gui.FindWindow(None, to_who)
        # 向窗口发送 ‘@QQ’
        win32gui.SendMessage(qq,770,0,0)

        # 太快反应不过来 放到写成语的前边
        # 模拟回车键 选择@QQ小冰
        # win32gui.SendMessage(qq,win32con.WM_KEYDOWN,win32con.VK_RETURN,0)

    def keyPressEvent(self, QKeyEvent):
        if QKeyEvent.key()==QtCore.Qt.Key_Return:
            self.sendtoxb()

if __name__=='__main__':
    app=QtWidgets.QApplication(sys.argv)
    cyjl=mycyjl()
    cyjl.show()
    sys.exit(app.exec_())